import {defineStore} from "pinia";
import {ref} from 'vue';
import {
    useDeleteOneByIdAPI,
    useGetAllCountAPI,
    useGetAllMaterialAPI,
    useGetByMultifunctionAPI,
    useGetByTypeAPI, useGetCountByMultifunctionNameAPI,
    useGetCountsByTypeNameAPI, useGetOneByIdAPI, useModifyMaterialAPI, useModifyStatusAPI
} from '@/apis/material.ts';
import {RouteParamValue} from "vue-router";




export const useMaterialStore=defineStore('material',()=>{

//     所有材料信息数据的state
    const allMaterialByPage=ref([]);
    const allMaterialCount=ref(0);

    const oneMaterial=ref({});

    const getAllMaterialByPageStore=async (current:number,pageSize:number)=>{
        const res=await useGetAllMaterialAPI(current,pageSize);
        allMaterialByPage.value=res.data;
        allMaterialCount.value=(await  useGetAllCountAPI()).data
    }

    // 根据材料类型查询材料数据
    const getMaterialByTypeStore=async (typeName:string,orderBy:number,current:number,pageSize:number)=>{
        const res=await useGetByTypeAPI(typeName,orderBy,current,pageSize);
        allMaterialByPage.value=res.data;
        allMaterialCount.value=(await useGetCountsByTypeNameAPI(typeName)).data

    }

    // 根据材料类型查询材料数据获取所有条数
    const getMaterialCountsByTypeNameStore=async (typeName:string)=>{
        const res=await useGetCountsByTypeNameAPI(typeName);
        allMaterialCount.value=res.data;
    }


    // 获取所有数据的条数
    const getAllCountStore=async ()=>{
        const res=await useGetAllCountAPI();
        allMaterialCount.value=res.data;
    }

    //  多功能查询接口,包含数据行条数

    const getByMultifunctionStore=async (name:string,current:number,pageSize:number)=>{
        const res=await useGetByMultifunctionAPI(name,current,pageSize);
        allMaterialByPage.value=res.data;
        const resCount= await useGetCountByMultifunctionNameAPI(name);
        allMaterialCount.value=resCount.data;
    }

    // 修改一条信息状态
    const modifyMaterialStatus=async (formData:FormData)=>{
        await useModifyStatusAPI(formData);
    }

//     删除一条信息
    const deleteOneById=async (id:number)=>{
        await useDeleteOneByIdAPI(id);
    }

//     通过id获取一条材料信息
    const getOneByIdStore=async (id: string | RouteParamValue[])=>{
        const res=  await useGetOneByIdAPI(id);
        oneMaterial.value=res.data;
    }

//     修改一条材料信息
    const modifyMaterialStore=async (formData:FormData)=>{
        await useModifyMaterialAPI(formData);
    }





// 配合多功能查询接口获取数据条数

    return {
        allMaterialByPage,getAllMaterialByPageStore,getMaterialByTypeStore,getAllCountStore,allMaterialCount,
        getMaterialCountsByTypeNameStore,getByMultifunctionStore,modifyMaterialStatus,deleteOneById,
        oneMaterial,getOneByIdStore,modifyMaterialStore
    }
})